package kr.ac.mju.mdc.mjuapp;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

import kr.ac.mju.mdc.mjuapp.campusmap.CampusmapActivity;
import kr.ac.mju.mdc.mjuapp.cipher.CipherManager;
import kr.ac.mju.mdc.mjuapp.community.CommunityActivity;
import kr.ac.mju.mdc.mjuapp.complaint.ComplaintListActivity;
import kr.ac.mju.mdc.mjuapp.food.FoodActivity;
import kr.ac.mju.mdc.mjuapp.green.GreencampusMainActivity;
import kr.ac.mju.mdc.mjuapp.help.HelpViewActivity;
import kr.ac.mju.mdc.mjuapp.information.InformationViewActivity;
import kr.ac.mju.mdc.mjuapp.introduce.IntroduceActivity;
import kr.ac.mju.mdc.mjuapp.login.LoginActivity;
import kr.ac.mju.mdc.mjuapp.login.LoginManager;
import kr.ac.mju.mdc.mjuapp.login.LoginThread;
import kr.ac.mju.mdc.mjuapp.myiweb.MyiwebActivity;
import kr.ac.mju.mdc.mjuapp.network.NetworkManager;
import kr.ac.mju.mdc.mjuapp.notice.NoticeListActivity;
import kr.ac.mju.mdc.mjuapp.photosns.PhotoSNSInfo;
import kr.ac.mju.mdc.mjuapp.photosns.PhotoSNSTextThread;
import kr.ac.mju.mdc.mjuapp.photosns.PhotoSNSThread;
import kr.ac.mju.mdc.mjuapp.photosns.PhotoSNSWriteActivity;
import kr.ac.mju.mdc.mjuapp.traffic.TrafficActivity;
import kr.ac.mju.mdc.mjuapp.util.PixelConverter;
import kr.ac.mju.mdc.mjuapp.weather.WeatherInfo;
import kr.ac.mju.mdc.mjuapp.weather.WeatherThread;
import kr.ac.mju.mdc.mjuapp.web.WebViewActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.webkit.CookieManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author davidkim
 *
 */
public class MainActivity extends Activity implements OnClickListener {
	public static final int WEATHER_SEOUL_COMPLETE = 0X01;
	public static final int WEATHER_YONGIN_COMPLETE = 0X03;
	public static final int WEATHER_FAIL = 0X04;
	public static final int IMAGE_BOARD_COMPLETE = 0X05;
	public static final int IMAGE_BOARD_FAIL = 0X06;
	public static final int IMAGE_BOARD_TEXT_COMPLETE = 0x07;
	public static final int IMAGE_BOARD_TEXT_FAIL = 0x08;
	public static final int IMAGE_BOARD_START_TIMER = 0x09;

	private final int MENU_HELP = 0;
	private final int MENU_INFORMATION = 1;

	private final int LOAD_COMPLETE = 0X20;
	private final int SPLASH_DISMISS = 0x30;
	private final int RQ_WRITE_IMG_BRD_AFTER_LOGIN = 0x41;
	private final int RQ_MYIWEB_AFTER_LOGIN = 0x42;

	private final int LOAD_THREAD_COUNT = 3;
	private final int LOAD_FAIL_COUNT = 0;

	private boolean isOnCreate = true;

	private AtomicInteger loadCount = new AtomicInteger();

	private ArrayList<PhotoSNSInfo> textInfoList = new ArrayList<PhotoSNSInfo>();

	private Timer imageBoardTimer = null;
	private TimerTask imageBoardTimerTask = null;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			int what = msg.what;
			switch (what) {
			case LOAD_COMPLETE:
				if (loadCount.decrementAndGet() <= LOAD_FAIL_COUNT) {
					if (isOnCreate)
						handler.sendEmptyMessageDelayed(SPLASH_DISMISS, 1000);
				}
				break;
			case WEATHER_SEOUL_COMPLETE:
				handler.sendEmptyMessage(LOAD_COMPLETE);
				// get weather info
				WeatherInfo swi = (WeatherInfo) msg.obj;
				String sTemp = swi.getTemp();
				String sText = swi.getText();
				// set weather info
				((TextView) findViewById(R.id.main_top_seoul_weather_degree)).setText(sTemp + "˚");
				if (sText.equals("맑음"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_clear);
				else if (sText.equals("구름 조금"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_partly_cloudy);
				else if (sText.equals("구름 많음"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_mostly_cloudy);
				else if (sText.equals("흐림"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_cloudy);
				else if (sText.equals("비"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_rain);
				else if (sText.equals("눈/비"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_snow_rain);
				else if (sText.equals("눈"))
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_snow);
				else
					((ImageView) findViewById(R.id.main_top_seoul_weather_image)).setImageResource(R.drawable.icon_weather_clear);
				break;
			case WEATHER_YONGIN_COMPLETE:
				handler.sendEmptyMessage(LOAD_COMPLETE);
				// get weather info
				WeatherInfo ywi = (WeatherInfo) msg.obj;
				String yTemp = ywi.getTemp();
				String yText = ywi.getText();
				// set weather info
				((TextView) findViewById(R.id.main_top_yongin_weather_degree)).setText(yTemp + "˚");
				if (yText.equals("맑음"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_clear);
				else if (yText.equals("구름 조금"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_partly_cloudy);
				else if (yText.equals("구름 많음"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_mostly_cloudy);
				else if (yText.equals("흐림"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_cloudy);
				else if (yText.equals("비"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_rain);
				else if (yText.equals("눈/비"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_snow_rain);
				else if (yText.equals("눈"))
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_snow);
				else
					((ImageView) findViewById(R.id.main_top_yongin_weather_image)).setImageResource(R.drawable.icon_weather_clear);
				break;
			case IMAGE_BOARD_COMPLETE:
				Bitmap bitmap = (Bitmap) msg.obj;
				ViewPager imageboardViewPager = ((ViewPager) findViewById(R.id.main_top_image_viewpager));
				((MainImgBrdPagerAdapter) imageboardViewPager.getAdapter()).addView(bitmap);
				break;
			case LoginActivity.LOGIN_SUCCESS:
				handler.sendEmptyMessage(LOAD_COMPLETE);
				break;
			case IMAGE_BOARD_TEXT_COMPLETE:
				textInfoList = (ArrayList<PhotoSNSInfo>) msg.obj;
				break;
			case IMAGE_BOARD_START_TIMER:
				startImageBoardTimer();
				break;
			case WEATHER_FAIL:
				handler.sendEmptyMessage(LOAD_COMPLETE);
				break;
			case IMAGE_BOARD_FAIL:
				// do nothing
				break;
			case LoginActivity.LOGIN_FAIL:
				handler.sendEmptyMessage(LOAD_COMPLETE);
				break;
			case IMAGE_BOARD_TEXT_FAIL:
				//do nothing
				break;
			case SPLASH_DISMISS:
				Animation fadeOutAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.splash_fade_out);
				fadeOutAnim.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						// set onCreate false, invisible splash
						isOnCreate = false;
						findViewById(R.id.main_splash_image).setVisibility(View.INVISIBLE);
						setBtnListener();
					}
				});
				findViewById(R.id.main_splash_image).startAnimation(fadeOutAnim);
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		// init layout
		initLayout();
		// init
		init();
		// getData
		getData();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//start image board paging timer(when main activity came back from other activities)
		if (((ViewPager) findViewById(R.id.main_top_image_viewpager)).getChildCount() > 0)
			handler.sendEmptyMessage(IMAGE_BOARD_START_TIMER);
		// change login btn
		changeLoginBtn();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		stopImageBoardTimer();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// logout
		if (LoginManager.checkLogin(getApplicationContext())) {
			// get pref
			SharedPreferences pref = getSharedPreferences(getString(R.string.pref_name), MODE_PRIVATE);
			boolean checkAutoLogin = pref.getBoolean(getString(R.string.pref_key_auto_login), false);
			// logout
			if (!checkAutoLogin)
				logout();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			if (requestCode == RQ_WRITE_IMG_BRD_AFTER_LOGIN) {
				findViewById(R.id.main_btn_camera).performClick();
			} else if (requestCode == RQ_MYIWEB_AFTER_LOGIN) {
				ViewPager btnPager = (ViewPager) findViewById(R.id.main_bottom_btn_viewpager);
				btnPager.getChildAt(0).findViewById(R.id.main_btn_myiweb).performClick();
			}
		}
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		super.onCreateOptionsMenu(menu);
//		menu.setQwertyMode(true);
//		menu.add(0, MENU_HELP, 1, "도움말");
//		menu.add(0, MENU_INFORMATION, 2, "어플리케이션 정보");
//		return true;
//	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		Intent intent = null;
//		switch (item.getItemId()) {
//		case MENU_HELP:
//			intent = new Intent(MainActivity.this, HelpViewActivity.class);
//			startActivity(intent);
//			return true;
//		case MENU_INFORMATION:
//			intent = new Intent(MainActivity.this, InformationViewActivity.class);
//			startActivity(intent);
//			return true;
//		}
//		return false;
//	}

	/**
	 * 
	 */
	private void initPageMark(int position) {
		// remove all mark
		((LinearLayout) findViewById(R.id.main_bottom_page_mark_layout)).removeAllViews();
		// pixel converter
		PixelConverter converter = new PixelConverter(getApplicationContext());
		// add mark
		for (int index = 0; index < 2; index++) {
			ImageView mark = new ImageView(getApplicationContext());
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(converter.getWidth(20), LinearLayout.LayoutParams.FILL_PARENT);
			mark.setLayoutParams(params);
			if (index == position)
				mark.setBackgroundResource(R.drawable.icon_pagemark_on);
			else
				mark.setBackgroundResource(R.drawable.icon_pagemark_off);
			((LinearLayout) findViewById(R.id.main_bottom_page_mark_layout)).addView(mark);
		}
	}

	/**
	 * 
	 */
	private void init() {
		// set imageboardViewPager
		ViewPager imgBrdViewPager = ((ViewPager) findViewById(R.id.main_top_image_viewpager));
		imgBrdViewPager.setAdapter(new MainImgBrdPagerAdapter(getApplicationContext(), handler));
		findViewById(R.id.main_top_defulat_image).setVisibility(View.VISIBLE);
		imgBrdViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				if (textInfoList.size() > 0) {
					((TextView) findViewById(R.id.main_top_imageboard_name)).setText(textInfoList.get(position).getName());
					((TextView) findViewById(R.id.main_top_imageboard_title)).setText(textInfoList.get(position).getTitle());
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		// set btnViewPager
		ViewPager btnViewPager = (ViewPager) findViewById(R.id.main_bottom_btn_viewpager);
		MainBtnPagerAdapter btnPagerAdapter = new MainBtnPagerAdapter(getApplicationContext());
		btnViewPager.setAdapter(btnPagerAdapter);
		btnPagerAdapter.notifyDataSetChanged();
		btnViewPager.setCurrentItem(0);
		initPageMark(0);
		btnViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				initPageMark(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 
	 */
	private void initLayout() {
		// set pixelConverter
		PixelConverter converter = new PixelConverter(getApplicationContext());
		LinearLayout.LayoutParams params = null;
		RelativeLayout.LayoutParams rParams = null;
		// main top layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_top_layout).getLayoutParams();
		rParams.height = converter.getHeight(251);
		(findViewById(R.id.main_top_layout)).setLayoutParams(rParams);
		// main top left layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_top_left_layout).getLayoutParams();
		rParams.width = converter.getWidth(319);
		findViewById(R.id.main_top_left_layout).setLayoutParams(rParams);
		// main top btn camera layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_btn_camera).getLayoutParams();
		rParams.width = converter.getWidth(95);
		rParams.height = converter.getHeight(63);
		// main top right layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_top_right_layout).getLayoutParams();
		rParams.width = converter.getWidth(162);
		findViewById(R.id.main_top_right_layout).setLayoutParams(rParams);
		// main top logo layout
		params = (LayoutParams) findViewById(R.id.main_top_logo_layout).getLayoutParams();
		params.height = converter.getHeight(50);
		findViewById(R.id.main_top_logo_layout).setLayoutParams(params);
		// main top weather layout
		params = (LayoutParams) findViewById(R.id.main_top_weather_layout).getLayoutParams();
		params.height = converter.getHeight(122);
		findViewById(R.id.main_top_weather_layout).setLayoutParams(params);
		// main top seoul weather layout
		params = (LayoutParams) findViewById(R.id.main_top_seoul_weather_layout).getLayoutParams();
		params.width = converter.getWidth(81);
		findViewById(R.id.main_top_seoul_weather_layout).setLayoutParams(params);
		// typeface
		Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/yungodic330.ttf");
		// main top seoul weather degree
		params = (LayoutParams) findViewById(R.id.main_top_seoul_weather_degree).getLayoutParams();
		params.height = converter.getHeight(27);
		findViewById(R.id.main_top_seoul_weather_degree).setLayoutParams(params);
		findViewById(R.id.main_top_seoul_weather_degree).setPadding(0, converter.getHeight(8), converter.getWidth(4), 0);
		((TextView) findViewById(R.id.main_top_seoul_weather_degree)).setTypeface(tf);
		// main top seoul weather image
		params = (LayoutParams) findViewById(R.id.main_top_seoul_weather_image).getLayoutParams();
		params.height = converter.getHeight(59);
		findViewById(R.id.main_top_seoul_weather_image).setLayoutParams(params);
		// main top yongin weather layout
		params = (LayoutParams) findViewById(R.id.main_top_yongin_weather_layout).getLayoutParams();
		params.width = converter.getWidth(81);
		findViewById(R.id.main_top_yongin_weather_layout).setLayoutParams(params);
		// main top yongin weather degree
		params = (LayoutParams) findViewById(R.id.main_top_yongin_weather_degree).getLayoutParams();
		params.height = converter.getHeight(27);
		findViewById(R.id.main_top_yongin_weather_degree).setLayoutParams(params);
		findViewById(R.id.main_top_yongin_weather_degree).setPadding(0, converter.getHeight(8), converter.getWidth(4), 0);
		((TextView) findViewById(R.id.main_top_yongin_weather_degree)).setTypeface(tf);
		// main top yongin weather image
		params = (LayoutParams) findViewById(R.id.main_top_yongin_weather_image).getLayoutParams();
		params.height = converter.getHeight(59);
		findViewById(R.id.main_top_yongin_weather_image).setLayoutParams(params);
		// main top text content layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_top_text_content_layout).getLayoutParams();
		rParams.width = converter.getWidth(168);
		rParams.height = converter.getHeight(80);
		findViewById(R.id.main_top_text_content_layout).setLayoutParams(rParams);
		// typeface 1
		Typeface tf1 = Typeface.createFromAsset(getAssets(), "fonts/yungodic320.ttf");
		// main top image board text name
		((TextView) findViewById(R.id.main_top_imageboard_name)).setTypeface(tf);
		// main top image board text title
		((TextView) findViewById(R.id.main_top_imageboard_title)).setTypeface(tf1);
		// main bottom layout
		rParams = (android.widget.RelativeLayout.LayoutParams) findViewById(R.id.main_bottom_layout).getLayoutParams();
		rParams.height = converter.getHeight(517);
		findViewById(R.id.main_bottom_layout).setLayoutParams(rParams);
		// main bottom page mark layout
		params = (LayoutParams) findViewById(R.id.main_bottom_page_mark_layout).getLayoutParams();
		params.height = converter.getHeight(25);
		findViewById(R.id.main_bottom_page_mark_layout).setLayoutParams(params);
		// main bottom btn viewpager
		params = (LayoutParams) findViewById(R.id.main_bottom_btn_viewpager).getLayoutParams();
		params.height = converter.getHeight(421);
		findViewById(R.id.main_bottom_btn_viewpager).setLayoutParams(params);
		// main bottom btn layout
		params = (LayoutParams) findViewById(R.id.main_bottom_btn_layout).getLayoutParams();
		params.height = converter.getHeight(71);
		findViewById(R.id.main_bottom_btn_layout).setLayoutParams(params);
		findViewById(R.id.main_bottom_btn_layout).setPadding(converter.getWidth(19), converter.getHeight(8), converter.getWidth(19),
				converter.getHeight(14));
		// main btn greencampus
		params = (LayoutParams) findViewById(R.id.main_btn_greencampus).getLayoutParams();
		params.width = converter.getWidth(292);
		params.rightMargin = converter.getWidth(8);
		findViewById(R.id.main_btn_greencampus).setLayoutParams(params);
		// main btn login
		params = (LayoutParams) findViewById(R.id.main_btn_login).getLayoutParams();
		params.width = converter.getWidth(142);
		findViewById(R.id.main_btn_login).setLayoutParams(params);
	}

	/**
	 * 
	 */
	private void getData() {
		if (NetworkManager.checkNetwork(getApplicationContext())) {
			if (checkAutoLogin()) {
				loadCount.set(LOAD_THREAD_COUNT);
				getLogin();
			} else
				loadCount.set(LOAD_THREAD_COUNT - 1);
			getWeather();
			getImageBoard();
		} else {
			loadCount.set(LOAD_FAIL_COUNT);
			handler.sendEmptyMessage(LOAD_COMPLETE);
		}
	}

	/**
	 * 
	 */
	private void getWeather() {
		WeatherThread seoulWeather = new WeatherThread(handler, getApplicationContext(), WeatherThread.TYPE_SEOUL);
		seoulWeather.start();
		WeatherThread yonginWeather = new WeatherThread(handler, getApplicationContext(), WeatherThread.TYPE_YONGIN);
		yonginWeather.start();
	}

	/**
	 * 
	 */
	private void getLogin() {
		// get Preferences
		SharedPreferences pref = getSharedPreferences(getString(R.string.pref_name), MODE_PRIVATE);
		String id = pref.getString(getString(R.string.pref_key_user_id), null);
		String pw = pref.getString(getString(R.string.pref_key_user_pw), null);
		// decrypt password
		pw = CipherManager.decryptDES(pw, getApplicationContext());
		LoginThread loginThread = new LoginThread(getApplicationContext(), handler, LoginActivity.LOGIN_TYPE_MOBILE, id, pw);
		loginThread.start();
	}

	/**
	 * 
	 */
	private void getImageBoard() {
		// init vairable
		textInfoList.clear();
		((ViewPager) findViewById(R.id.main_top_image_viewpager)).removeAllViews();
		((MainImgBrdPagerAdapter) (((ViewPager) findViewById(R.id.main_top_image_viewpager)).getAdapter())).clear();
		// start threads
		PhotoSNSTextThread imgBrdTextThread = new PhotoSNSTextThread(handler);
		imgBrdTextThread.start();
		PhotoSNSThread imgBrdThread = new PhotoSNSThread(handler);
		imgBrdThread.start();
	}

	/**
	 * 
	 */
	private void setBtnListener() {
		// main top
		findViewById(R.id.main_btn_camera).setOnClickListener(this);
		// main bottom
		findViewById(R.id.main_btn_login).setOnClickListener(this);
		findViewById(R.id.main_btn_greencampus).setOnClickListener(this);
		ViewPager btnViewPager = (ViewPager) findViewById(R.id.main_bottom_btn_viewpager);
		// first layout
		View view = btnViewPager.getChildAt(0);
		view.findViewById(R.id.main_btn_myiweb).setOnClickListener(this);
		view.findViewById(R.id.main_btn_introduce).setOnClickListener(this);
		view.findViewById(R.id.main_btn_library).setOnClickListener(this);
		view.findViewById(R.id.main_btn_notice).setOnClickListener(this);
		view.findViewById(R.id.main_btn_complaint).setOnClickListener(this);
		view.findViewById(R.id.main_btn_food).setOnClickListener(this);
		view.findViewById(R.id.main_btn_admission).setOnClickListener(this);
		// second layout
		view = btnViewPager.getChildAt(1);
		view.findViewById(R.id.main_btn_campusmap).setOnClickListener(this);
		view.findViewById(R.id.main_btn_community).setOnClickListener(this);
		view.findViewById(R.id.main_btn_phoneinfo).setOnClickListener(this);
		view.findViewById(R.id.main_btn_traffic).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		Intent intent = null;

		switch (id) {
		case R.id.main_btn_camera:
			if (NetworkManager.checkNetwork(MainActivity.this)) {
				if (LoginManager.checkLogin(MainActivity.this)) {
					intent = new Intent(MainActivity.this, PhotoSNSWriteActivity.class);
					break;
				} else {
					intent = new Intent(MainActivity.this, LoginActivity.class);
					startActivityForResult(intent, RQ_WRITE_IMG_BRD_AFTER_LOGIN);
					return;
				}
			}
			return;
		case R.id.main_btn_login:
			// check network
			if (NetworkManager.checkNetwork(getApplicationContext())) {
				// login
				if (!LoginManager.checkLogin(getApplicationContext()))
					intent = new Intent(MainActivity.this, LoginActivity.class);
				// logout
				else {
					logout();
					changeLoginBtn();
				}
			}
			break;
		case R.id.main_btn_greencampus:
			intent = new Intent(MainActivity.this, GreencampusMainActivity.class);
			break;
		case R.id.main_btn_myiweb:
			if (NetworkManager.checkNetwork(MainActivity.this)) {
				if (LoginManager.checkLogin(MainActivity.this)) {
					intent = new Intent(MainActivity.this, MyiwebActivity.class);
					break;
				} else {
					intent = new Intent(MainActivity.this, LoginActivity.class);
					startActivityForResult(intent, RQ_MYIWEB_AFTER_LOGIN);
					return;
				}
			}
			return;
			// break;
		case R.id.main_btn_introduce:
			intent = new Intent(MainActivity.this, IntroduceActivity.class);
			break;
		case R.id.main_btn_library:
			intent = new Intent(MainActivity.this, WebViewActivity.class);
			intent.putExtra("rooturl", getResources().getString(R.string.url_library));
			break;
		case R.id.main_btn_notice:
			intent = new Intent(MainActivity.this, NoticeListActivity.class);
			break;
		case R.id.main_btn_complaint:
			intent = new Intent(MainActivity.this, ComplaintListActivity.class);
			break;
		case R.id.main_btn_food:
			intent = new Intent(MainActivity.this, FoodActivity.class);
			break;
		case R.id.main_btn_campusmap:
			intent = new Intent(MainActivity.this, CampusmapActivity.class);
			break;
		case R.id.main_btn_admission:
			// check entrance application installed
			if (isAdmissionInstalled()) {
				// start AdmissionApp
				intent = getPackageManager().getLaunchIntentForPackage(getString(R.string.url_entrance_app_pkg_name));
			} else {
				// start market
				Uri uri = Uri.parse(getString(R.string.url_entrance_app_market));
				intent = new Intent(Intent.ACTION_VIEW, uri);
			}
			break;
		case R.id.main_btn_community:
			intent = new Intent(MainActivity.this, CommunityActivity.class);
			break;
		case R.id.main_btn_phoneinfo:
			intent = new Intent(MainActivity.this, WebViewActivity.class);
			intent.putExtra("rooturl", getResources().getString(R.string.url_phone));
			break;
		case R.id.main_btn_traffic:
			intent = new Intent(MainActivity.this, TrafficActivity.class);
			break;
		}
		if (intent != null) {
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);
		}
	}

	/**
	 * @return
	 */
	private boolean checkAutoLogin() {
		// get
		SharedPreferences pref = getSharedPreferences(getString(R.string.pref_name), MODE_PRIVATE);
		boolean checkAutoLogin = pref.getBoolean(getString(R.string.pref_key_auto_login), false);
		return checkAutoLogin;
	}

	/**
	 * 
	 */
	private void changeLoginBtn() {
		// check login
		if (LoginManager.checkLogin(getApplicationContext()))
			((Button) findViewById(R.id.main_btn_login)).setBackgroundResource(R.drawable.btn_logout_selector);
		else
			((Button) findViewById(R.id.main_btn_login)).setBackgroundResource(R.drawable.btn_login_selector);
	}

	/**
	 * 
	 */
	private void logout() {
		// cookie remove
		CookieManager.getInstance().removeAllCookie();
		Toast.makeText(getApplicationContext(), "로그아웃 되었습니다", Toast.LENGTH_SHORT).show();
		// get prefeditor
		SharedPreferences pref = getSharedPreferences(getString(R.string.pref_name), MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		// set pref
		editor.putBoolean(getString(R.string.pref_key_auto_login), false);
		editor.putString(getString(R.string.pref_key_user_pw), null);
		editor.commit();
	}

	/**
	 * @return
	 */
	private boolean isAdmissionInstalled() {
		List<PackageInfo> appinfo = getPackageManager().getInstalledPackages(0);
		for (int i = 0; i < appinfo.size(); i++) {
			PackageInfo pi = appinfo.get(i);
			if (pi.packageName.equals(getString(R.string.url_entrance_app_pkg_name)))
				return true;
		}
		return false;
	}

	/**
	 * 
	 */
	private void startImageBoardTimer() {
		if (imageBoardTimer == null) {
			imageBoardTimer = new Timer();
			imageBoardTimerTask = new TimerTask() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					handler.post(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							ViewPager imageBoardPager = (ViewPager) findViewById(R.id.main_top_image_viewpager);
							int currentPage = imageBoardPager.getCurrentItem();
							int currentSize = imageBoardPager.getAdapter().getCount();
							int changePage = (currentPage + 1) % currentSize;
							if (imageBoardPager.getChildCount() > 0)
								imageBoardPager.setCurrentItem(changePage);
							if (textInfoList.size() > changePage) {
								((TextView) findViewById(R.id.main_top_imageboard_name)).setText(textInfoList.get(changePage).getName());
								((TextView) findViewById(R.id.main_top_imageboard_title)).setText(textInfoList.get(changePage).getTitle());
							}
							findViewById(R.id.main_top_defulat_image).setVisibility(View.INVISIBLE);
						}
					});
				}
			};
			imageBoardTimer.schedule(imageBoardTimerTask, 0, 4000);
		}
	}

	/**
	 * 
	 */
	private void stopImageBoardTimer() {
		if (imageBoardTimerTask != null) {
			imageBoardTimerTask.cancel();
			imageBoardTimerTask = null;
		}
		if (imageBoardTimer != null) {
			imageBoardTimer.purge();
			imageBoardTimer.cancel();
			imageBoardTimer = null;
		}
	}
}
/* end of file */